package com.itluma.isWOak.controller;

import com.github.pagehelper.PageInfo;
import com.itluma.isWOak.pojo.AdminInfo;
import com.itluma.isWOak.pojo.ModuleInfo;
import com.itluma.isWOak.pojo.RoleInfo;
import com.itluma.isWOak.service.AdminInfoService;
import com.itluma.isWOak.service.AdminRoleService;
import com.itluma.isWOak.service.ModuleInfoService;
import com.itluma.isWOak.service.RoleService;
import com.itluma.isWOak.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminInfoController {
    @Autowired
    private AdminInfoService adminInfoService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AdminRoleService adminRoleService;
    @Autowired
    private ModuleInfoService moduleInfoService;
    @RequestMapping("/admin_list")
    public String page(Model model, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "limit", defaultValue = "2") Integer limit, Integer moduleId, String roleName){
        PageInfo<AdminInfo> pageInfo = adminInfoService.page(pageNum, limit, moduleId, roleName);
        List<RoleInfo> roleInfos = roleService.list();
        List<ModuleInfo> moduleInfos = moduleInfoService.findAll();
        model.addAttribute("pageInfo", pageInfo);
      //  model.addAttribute("roleInfos", roleInfos);
        model.addAttribute("moduleInfos", moduleInfos);
        model.addAttribute("moduleId", moduleId);
        model.addAttribute("roleName", roleName);
        return "admin/admin_list";
    }

    @GetMapping ("/admin_modi")
    public String modify(Model model, Integer id){
        AdminInfo adminInfo = adminInfoService.findById(id);
        List<RoleInfo> roleInfos = roleService.list();
        model.addAttribute("adminInfo", adminInfo);
        model.addAttribute("roleInfos", roleInfos);
        return "/admin/admin_modi";
    }
    @RequestMapping("/reset")
    @ResponseBody
    public ResultInfo reset(@RequestBody Integer[] ids){
        boolean flag = adminInfoService.resetPassword(ids);
        ResultInfo resultInfo = new ResultInfo(0, "重置密码成功", null);
        if(!flag){
            resultInfo.setCode(1);
            resultInfo.setMessage("操作失败");
        }
        return resultInfo;
    }

    @PostMapping("admin_modi")
    @Transactional
    public String modify(AdminInfo adminInfo, Integer... roles){
        adminInfoService.modify(adminInfo);
        adminRoleService.modifyAdminRole(adminInfo.getAdminId(), roles);
        return "redirect:/admin/admin_list";
    }

    @GetMapping("/admin_add")
    public String addPage(Model model){
        List<RoleInfo> roleInfos = roleService.list();
        model.addAttribute("roleInfos", roleInfos);
        return "/admin/admin_add";
    }
    @PostMapping("/admin_add")
    @Transactional
    public String add(AdminInfo adminInfo, Integer... roles){
        adminInfoService.add(adminInfo);
        adminRoleService.modifyAdminRole(adminInfo.getAdminId(), roles);

        return "redirect:/admin/admin_list";
    }
    @RequestMapping("/delete")
    public String delete(Integer adminId){
        adminInfoService.deleteByAdminId(adminId);
        return "redirect:/admin/admin_list";
    }

    @RequestMapping("/check")
    @ResponseBody
    public ResultInfo check(String adminCode){
        ResultInfo resultInfo = new ResultInfo(1, "账号名不可用", null);
        if(adminCode == null || adminCode.trim().length() == 0){
            return new ResultInfo(1, "账号名不能为空", null);
        }
        if(adminCode != null && adminCode.trim().length() > 0) {
            AdminInfo adminInfo = adminInfoService.getByAdminCode(adminCode);
            if(adminInfo == null){
                resultInfo.setCode(0);
                resultInfo.setMessage("账号名可用");
                return resultInfo;
            }
        }
        return resultInfo;
    }

}
